package com.hepinghospital.user.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.base.BaseApplication;
import com.hepinghospital.user.util.ImageLoaderHelper;

import butterknife.BindView;
import cn.bertsir.zbar.utils.QRUtils;
import jz.joyoung.robot.util.UtilString;

/**
 * @Description: java类作用描述
 * @Author: lzp
 * @CreateDate: 2023/11/1 8:58
 * @UpdateRemark: 更新说明
 */
public class JiuzhenCodeActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_patient_name)
    TextView tvPatientName;
    @BindView(R.id.tv_card_num)
    TextView tvCardNum;
    @BindView(R.id.iv_bar_code)
    ImageView ivBarCode;
    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;


    private String patientName;
    private String cardNo;

    @Override
    protected void initViews() {
        tvTitle.setText("就诊码");
        tvPatientName.setText(patientName);
        tvCardNum.setText("就诊卡号：" + cardNo);

        ivQrCode.setImageBitmap(QRUtils.getInstance().createQRCode(cardNo, 600, 600));

        if (!UtilString.isEmpty(BaseApplication.avatar)) {
            ImageLoaderHelper.getInstance().setCirclePicture(mContext, ivHead, BaseApplication.avatar);
        } else {
            if ("女".equals(BaseApplication.sex)) {
                ivHead.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_head_user_woman));
            } else {
                ivHead.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_head_user_man));
            }
        }
//        ivBarCode.setImageBitmap(QRUtils.getInstance().createBarCodeWithText(mContext, "13123131241231", 600, 100));
//        Bitmap barCodeWithText = createBarCode(mContext, "12345678987654321", 800, 200);
//        ivBarCode.setImageBitmap(barCodeWithText);
    }

    @Override
    protected void initData() {
        patientName = getIntent().getStringExtra("patientName");
        cardNo = getIntent().getStringExtra("cardNo");
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_jiuzhen_code;
    }


    public Bitmap createBarCode(Context context, String contents, int desiredWidth,
                                int desiredHeight) {
        if (TextUtils.isEmpty(contents)) {
            throw new NullPointerException("contents not be null");
        }
        if (desiredWidth == 0 || desiredHeight == 0) {
            throw new NullPointerException("desiredWidth or desiredHeight not be null");
        }
        Bitmap resultBitmap;

        /**
         * 条形码的编码类型
         */
        BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;

        Bitmap barcodeBitmap = encodeAsBitmap(contents, barcodeFormat,
                desiredWidth, desiredHeight);
        return barcodeBitmap;
    }

    private Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int desiredWidth, int desiredHeight) {
        final int WHITE = 0xFFFFFFFF;
        final int BLACK = 0xFF000000;

        MultiFormatWriter writer = new MultiFormatWriter();
        BitMatrix result = null;
        try {
            result = writer.encode(contents, format, desiredWidth,
                    desiredHeight, null);
        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int width = result.getWidth();
        int height = result.getHeight();
        int[] pixels = new int[width * height];
        // All are 0, or black, by default
        for (int y = 0; y < height; y++) {
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;

    }

}
